﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;

namespace WXYW
{
    public partial class AddAccessory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AccessoryService accessoryService = new AccessoryService();
                ClassDropdownList.DataSource = accessoryService.GetAccessoryTypeList(); 
                ClassDropdownList.DataTextField = "Name";
                ClassDropdownList.DataValueField = "ID";
                ClassDropdownList.DataBind();

                ServiceStationService stationService = new ServiceStationService();
                StationDropdownList.DataSource = stationService.GetALLServiceStations();
                StationDropdownList.DataTextField = "Name";
                StationDropdownList.DataValueField = "ID";
                StationDropdownList.DataBind();
            }
        }

        protected void SaveAddAccessoryBtn_Click(object sender, EventArgs e)
        {
            try
            {
                AccessoryService accessoryService = new AccessoryService();
                accessoryService.AddAccessory(MaterialNOTxtBox.Text, ModelNOTxtBox.Text, DescriptionTxtBox.Text,
                    Convert.ToDecimal(PriceTxtBox.Text), Convert.ToInt32(CountTxtBox.Text), Convert.ToInt32(ClassDropdownList.SelectedValue),
                    Convert.ToInt32(StationDropdownList.SelectedValue), ContextService.CurrentAccount);
                NewAccessoryMsgLbl.Text = "添加成功，请继续添加下一条。";
                ClearNewAccessoryForm();
            }
            catch (Exception ex)
            {
                NewAccessoryMsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }
        
        public void ClearNewAccessoryForm()
        {
            MaterialNOTxtBox.Text = string.Empty;
            ModelNOTxtBox.Text = string.Empty;
            DescriptionTxtBox.Text = string.Empty;
            CountTxtBox.Text = string.Empty;
            PriceTxtBox.Text = string.Empty;
        }
    }
}
